Page({

  /**
   * 页面的初始数据
   */
  data: {
    //倒计时
    text: '获取验证码',
    disabled:false,     //按钮是否禁用
    currentTime: 30,
    phone: '',          //获取到的手机栏中的值
    code: null,         //验证码
    code1:null,
    pass: null,
    pass1:null,
  },
  usernameInput: function(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  codeInput: function(e) {
    this.setData({
      code: e.detail.value
    })
  },
  resetPasswordInput: function(e) {
    this.setData({
      pass: e.detail.value
    })
  },
  resetPasswordVerify: function(e) {
    this.setData({
      pass1: e.detail.value
    })
  },
  countTime: function() {
    var inter = setInterval(function() {
      this.setData({
        disabled: true,
        sendColor: '#cccccc',
        text: this.data.currentTime + 's后重发',
        currentTime: this.data.currentTime - 1
      });
      if (this.data.currentTime < 0) {
        clearInterval(inter)
        this.setData({
          sendColor: 'rgb(60, 154, 231)',
          text: '获取验证码',
          currentTime: 30,
          disabled: false
        });
      }
    }.bind(this), 1000);
  },

  getCodeBtnClick: function() {
    if(this.data.phone==""||this.data.phone==undefined){
      wx.showToast({
        title: '请输入手机号',
        icon:'none'
      })
      return;
    }
    else if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(this.data.phone))){
      wx.showToast({
        title: '手机号格式错误',
        icon: 'none'
      })
      return;
    }
    this.countTime()
    var that = this
    wx.request({
      url: getApp().globalData.url+'assistantGetCode',
      method: 'GET',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        tel: this.data.phone,
      },
      success(res) {
        if (res.data == "false") {
          wx.showToast({
            title: '发送验证码失败，请检查手机号',
            icon: 'none'
          })
        } else {
          that.setData({
            code1: res.data
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: '请求失败，请重新请求',
          icon: 'none'
        })
      }
    })
  },
  
  registerinBtnClick:function(){
    if(this.data.pass!=this.data.pass1)
    {
      wx.showToast({
        title: '两次密码不一致',
        icon: 'none'
      })
      return;
    }
    else if(this.data.phone.length == 0 || this.data.pass.length == 0||this.data.pass1.length==0){
      wx.showToast({
        title: '手机号、密码不能为空',
        icon: 'none'
      })
    }else if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(this.data.phone))){
      wx.showToast({
        title: '手机号格式错误',
        icon: 'none'
      })
      return;
    }
    else if(this.data.code!=this.data.code1)
    {
      wx.showToast({
        title: '验证码错误',
        icon: 'none'
      })
      return;
    }
    else if(this.data.pass.length<=4||this.data.pass.length>=15)
    {
      wx.showToast({
        title: '密码长度必须在5-14位之间',
        icon: 'none'
      })
      return;
    }
    else {
      var password=/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).{5,14}$/;
      if(password.test(this.data.pass))
      {
        getApp().globalData.AssistantTel = this.data.phone ;
        getApp().globalData.AssistantPass = this.data.pass ;
        wx.navigateTo({
          url: '../assistantQualification/assistantQualification',
        })
      }
      else{
        wx.showToast({
          title: '密码必须包含大小写',
          icon: 'none',
        })  
      }
    }
  },
  
})
